<?php
    session_start();//setcookie();

    include "../mysql/mysql.php";
    $aa = new mysql;
    $aa->link("");
    include "../myfunction.php";
    $bb= new myfunction;

	
    $userid=$password='';
	$useriderr=$passworderr='';
	$re01=$re02=$re03=0; 
	  
    if ($_SERVER["REQUEST_METHOD"] == "POST"){ 
		//验证用户输入格式是否正确
		if (empty($_POST["userid"])){
			$useriderr = "用户名不能为空";
			}
		else{
			$userid = $bb->test_input($_POST["userid"]);
			if ($_POST["userid"] == ''
			//!preg_match("/^[0-9]{11}$/", $userid) && !preg_match("/^[A-Za-z0-9]+([_.][A-Za-z0-9]+)*@([A-Za-z0-9]+[_\.])+[A-Za-z0-9]{2-5}$/", $userid)
			){
				$useriderr = "您输入的用户名格式不正确";
				}
				else{
					$re01=1;
					}	
			}
			//验证用户密码输入格式是否正确
		if (empty($_POST["password"])){
			$passworderr = "密码不能为空";
			}
		else{
			$password = $bb->test_input($_POST["password"]);
	
			if (!preg_match("/^[A-Za-z0-9]{6,12}$/", $password) ){
				$passworderr = "密码6-12位，仅包含A-Za-z0-9";
				}
				else{
					$re02=1;
					}
			}
	 }
		//验证用户是否存在、密码是否正确
    if($re01 == 1 && $re02 == 1){
		
		$result= $bb->finduser($userid);

		if ($result['user_name']){
			$password = $bb->jiami($password);
			if ($password != $result['user_pw']){
				$passworderr = "密码错误请重新输入";
				}
				else{
					$re03=1;
					//用户名密码验证正确，检查和设置自动登录
					  if(!empty($_POST['autologin'])){
						  
                            setcookie("userid",$userid,time()+3600*24*7, "/");
                            setcookie("password",$password,time()+3600*24*7, "/");
							setcookie("cookie", "cookiethree");
                               
                            }
					
					
					}
		}
	    else{
           $useriderr = "您输入的用户名不存在";
	        }              
	  	
		
		
		if(@$re03 == 1){
			if(isset($_SESSION['userurl'])){
				$url= $_SESSION['userurl'];
			 }
			 else{
				$url= "../shoppingmall.php";
			}
			$_SESSION['userid']=$userid;	
			echo "<meta http-equiv=\"refresh\" content=\"0.5;url=$url\">";
							
			}  
		
		}



?>


<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>锤子科技商城登陆</title>
<style>
body{ background-color:#CFF;}
#tbody{ 
    width:1100px; 
	height: auto; 
	margin:0 auto; 
	padding-top: 10px;
}
#login{
	background-color: #FFF;
	width: 500px;
	margin: 200px auto;
	padding-top: 20px;
	padding-bottom: 30px;
	}
#login01{
	width: 100px;
	height: 100px;
	margin: 30px auto;
	padding: 0;
	}
#login01 img{
	width: 100px;
	height: 100px;
	}
#login02 img{
	width: 30px;
	height: 30px;
}
.h30{
	height: 25px;
	width:250px;
	border: none;
	}
#denglu{
	width:200px;
	height: 30px;
	background-color:#CCC;
	border: 1px solid #000;
	border-radius: 5px;
	line-height: 30px;
	}


table{ border-collapse: collapse;}
td{
padding: 0;}
tr:nth-child(odd){border: 1px solid #9FF;
padding: 0;}


.error {color: #FF0000;}


</style>


</head>

<body>

<div id="tbody">

    <div id="login">
        <div id="login01">
            <img src="../img/head-logo.jpg" />
        </div>
        <div id="login02">

             <form id="form_login" name="login" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
               <div style=" margin: 0 auto; width: 300px;">
                 <table>
                    <tr><td><img src="../img/loginuser.png" /></td><td><input type="email" name="userid" class="h30" placeholder="请输入一个邮箱" value="<?php echo $userid; ?>"></td><td><span class="error">*</span></td></tr>
                    <tr style=" height: 30px;"><td></td><td><span class="error"><small><?php echo $useriderr; ?></small></span></td></tr>
                    <tr><td><img src="../img/loginpassword.png" /></td><td><input type="password" name="password" class="h30" placeholder="密码6-12位" value="<?php //echo $password; ?>"></td><td><span class="error">*</span></td></tr>
                    <tr style=" height: 30px;"><td></td><td><span class="error"><small><?php echo $passworderr; ?></small></span></td></tr>
                 </table>
              </div>
                <div style=" margin: 0 auto; width: 300px;"><p><input type="checkbox" name="autologin"><small>7天内自动登录</small>　　　　　　<a href="register.php"><small>注册</small></a>|<a href="forgot.php"><small>忘记密码</small></a></p></div>
                <div style=" margin: 0 auto; width: 200px;"><input type="submit" name="submit" value="登　　陆" id="denglu" style='font-size:20px'></div>
            </form>
            
        </div>
    
    </div>









</div>
<?php
include "../mysql/mysqlclose.php";
?>
</body>
</html>